﻿using System.Collections.Generic;
using ku.datamodel.contract;
using ku.merger.contract;

namespace ku.merger
{
  public class Merger : IMerger
  {
    public IEnumerable<Token> Merge(IEnumerable<Token> tokens)
    {
      var previousToken = (Token)null;

      foreach (var token in tokens)
      {
        if (token.Typ == TokenType.Satzzeichen)
        {
          if (previousToken == null)
          {
            previousToken = token;
            continue;
          }
          if (previousToken.Typ != TokenType.Wort)
          {
            previousToken = token;
            continue;
          }
          yield return new Token(previousToken.Inhalt + token.Inhalt, TokenType.Wort);
          previousToken = null;
        }
        else
        {
          if (previousToken != null)
          {
            yield return previousToken;
          }
          previousToken = token;
        }
      }

      if (previousToken != null)
        yield return previousToken;


    }



  }
}
